package com.shujia.tuijian;

import com.sun.org.apache.bcel.internal.generic.ACONST_NULL;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;
import java.lang.management.LockInfo;

class MyMapper extends Mapper<LongWritable, Text, Text, LongWritable> {
    private Text mkey = new Text();
    private LongWritable mvalue = new LongWritable();


    @Override
    protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, LongWritable>.Context context) throws IOException, InterruptedException {
        //tom hello hadoop cat
        /**
         * tom:hello 1
         * tom:hadoop 1
         * tom:cat 1
         *
         * hello:hadoop -1
         * hello:cat -1
         * hadoop:cat -1
         */
        //hadoop tom hive world hello
        /**
         * hadoop:tom 1
         * hadoop:hive 1
         * hadoop:world 1
         * hello:hadoop 1
         */
        //将value类型的数据转成String类型
        String line = value.toString();
        String[] strings = line.split(" ");
        for (int i = 1; i < strings.length; i++) {
            //处理直接好友
            mkey.set(orderFriend(strings[0], strings[i]));
            mvalue.set(1L);
            context.write(mkey, mvalue);
            //处理间接好友
            for (int j = i + 1; j < strings.length; j++) {
                mkey.set(orderFriend(strings[i], strings[j]));
                mvalue.set(-1L);
                context.write(mkey, mvalue);
            }
        }
    }

    //目的是将map输出的key进行统一
    //hadoop:hello 1
    //hello:hadoop 1
    public static String orderFriend(String f1, String f2) {
        //比较字符串的大小
        int i = f1.compareTo(f2);
        if (i > 0) {
            return f1 + ":" + f2;
        } else {
            return f2 + ":" + f1;
        }
    }
}

class MyReducer extends Reducer<Text, LongWritable, Text, LongWritable> {
    //(hive:hello [-1,-1,1])
    @Override
    protected void reduce(Text key, Iterable<LongWritable> values, Reducer<Text, LongWritable, Text, LongWritable>.Context context) throws IOException, InterruptedException {
        //hadoop:hello 1
        //hadoop:hello -1
        int flag = 0;
        Long sum = 0L;
        for (LongWritable value : values) {
            if (value.get() == 1) {
                flag = 1;
            }
            sum += value.get();
        }

        if (flag == 0) {
            context.write(key, new LongWritable(-1L));
        } else {
            context.write(key, new LongWritable(0L));
        }
    }
}

/*
    计算出给出的数据中有多少直接关系的组合和间接关系的组合，
    将组合作为key
    直接关系，value是1
    间接关系，value是-1
 */
public class TuiJianDemo {
    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);
        job.setJarByClass(TuiJianDemo.class);
        job.setNumReduceTasks(1);
        job.setJobName("推荐任务");

        job.setMapperClass(MyMapper.class);
        job.setReducerClass(MyReducer.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(LongWritable.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);

        FileInputFormat.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        job.waitForCompletion(true);
    }
}
